/**
 * Created by lsx on 2017/5/19.
 */
require(['mui', 'jquery'], function (mui, $) {
    var store_id=$(".store.active")[0].attributes.ids.value;
    //购物数量的增减
    (function () {
        var up = document.querySelector('.icon-jia');
        var down = document.querySelector('.icon-jian');
        var value = document.querySelector('.value');
        up.addEventListener('tap', function () {
            if($(down).hasClass('end')){
                $(down).removeClass('end');
            }
            value.innerText = parseInt(value.innerText) + 1;
        })
        down.addEventListener('tap', function () {
            if (parseInt(value.innerText) > 1 && !$(down).hasClass('end')) {
                value.innerText = parseInt(value.innerText) - 1;
                if (parseInt(value.innerText) === 1) {
                    $(down).addClass('end')
                }
            }
        })
    })();
    //门店选择
    for(var i=0;i<document.querySelectorAll('.store').length;i++) {
        (function (i) {
            document.querySelectorAll('.store')[i].addEventListener('tap', function (e) {
                $(this).addClass('active').siblings().removeClass('active');
                // console.log($(this)[0].attributes.ids.value)
                store_id=$(this)[0].attributes.ids.value;
            })
        })(i)
    }



    //提交订单
    document.querySelector('.btn-submit').addEventListener('tap',function () {
        var address_id=$('#address_id').val();
        var order_count=$('#order_count')[0].innerText;
        var user_card_id=$("#watercard_id").val();
        $.ajax({
            dataType:'json',
            url:order_submit_url,
            data:{address_id:address_id,order_count:order_count,store_id:store_id,user_card_id:user_card_id},
            success:function (res) {
                if(res.success){
                    mui.toast(res.message);
                    var url=wc_order_detail_url+"/order_id/"+res.data.order_id;
                    console.log(url);
                    setTimeout("window.location.href='"+url+"'",1000);
                }
            }
        })

    })


})
